/**********************************************************************
 * Filename: Jogo.java
 * Created: 2012/04/30
 * @author Fernando Brito e Abreu
 **********************************************************************/
package org.quasar.CopaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;
import java.util.SortedSet;
import java.util.TreeSet;

public class Jogo {
	public static Set<Jogo> allInstances = new HashSet<Jogo>();

	private CalendarDate dia;
	private String hora;
	private Grupo grupo;
	private Estadio estadio;

	/**********************************************************************
	 * Default constructor
	 **********************************************************************/
	public Jogo() {
		allInstances.add(this);
	}

	/**********************************************************************
	 * Parameterized constructor
	 * 
	 * @param dia
	 *            the dia to initialize
	 * @param hora
	 *            the hora to initialize
	 * @param grupo
	 *            the grupo to initialize
	 * @param estadio
	 *            the estadio to initialize
	 **********************************************************************/
	public Jogo(CalendarDate dia, String hora, Grupo grupo, Estadio estadio) {
		this.dia = dia;
		this.hora = hora;
		this.grupo = grupo;
		this.estadio = estadio;
		allInstances.add(this);
	}

	/**********************************************************************
	 * Standard attribute getter
	 * 
	 * @return the dia of the jogo
	 **********************************************************************/
	public CalendarDate dia() {
		return dia;
	}

	/**********************************************************************
	 * Standard attribute setter
	 * 
	 * @param dia
	 *            the dia to set
	 **********************************************************************/
	public void setDia(CalendarDate dia) {
		this.dia = dia;
	}

	/**********************************************************************
	 * Standard attribute getter
	 * 
	 * @return the hora of the jogo
	 **********************************************************************/
	public String hora() {
		return hora;
	}

	/**********************************************************************
	 * Standard attribute setter
	 * 
	 * @param hora
	 *            the hora to set
	 **********************************************************************/
	public void setHora(String hora) {
		this.hora = hora;
	}

	/**********************************************************************
	 * ONE2MANY getter for Jogo[*] <-> Grupo[1]
	 * 
	 * @return the grupo of the jogo
	 **********************************************************************/
	public Grupo grupo() {
		return grupo;
	}

	/**********************************************************************
	 * ONE2MANY setter for Jogo[*] <-> Grupo[1]
	 * 
	 * @param grupo
	 *            the grupo to set
	 **********************************************************************/
	public void setGrupo(Grupo grupo) {
		this.grupo = grupo;
	}

	/**********************************************************************
	 * ONE2MANY getter for Jogo[*] <-> Estadio[1]
	 * 
	 * @return the estadio of the jogo
	 **********************************************************************/
	public Estadio estadio() {
		return estadio;
	}

	/**********************************************************************
	 * ONE2MANY setter for Jogo[*] <-> Estadio[1]
	 * 
	 * @param estadio
	 *            the estadio to set
	 **********************************************************************/
	public void setEstadio(Estadio estadio) {
		this.estadio = estadio;
	}

	/**********************************************************************
	 * MEMBER2ASSOCIATIVE getter for Jogo[*] <-> Equipa[2] ordered
	 * 
	 * @return the equipa of the jogo
	 **********************************************************************/
	public SortedSet<Equipa> equipa() {
		SortedSet<Equipa> result = new TreeSet<Equipa>();
		for (Equipa x : Equipa.allInstances)
			if (x.jogo() == this)
				result.add(x);
		return result;
	}

	/**********************************************************************
	 * MEMBER2ASSOCIATIVE setter for Jogo[*] <-> Equipa[2] ordered
	 * 
	 * @param equipa
	 *            the equipa to set
	 **********************************************************************/
	public void setEquipa(SortedSet<Equipa> equipa) {
		for (Equipa x : equipa)
			x.setJogo(this);
	}

	/**********************************************************************
	 * MEMBER2MEMBER getter for Jogo[*] <-> Pais[2] ordered
	 * 
	 * @return the seleccao of the jogo
	 **********************************************************************/
	public SortedSet<Pais> seleccao() {
		SortedSet<Pais> result = new TreeSet<Pais>();
		for (Equipa x : Equipa.allInstances)
			if (x.jogo() == this)
				result.add(x.seleccao());
		return result;
	}

	/**********************************************************************
	 * MEMBER2MEMBER setter for Jogo[*] <-> Pais[2] ordered
	 * 
	 * @param seleccao
	 *            the seleccao to set
	 **********************************************************************/
	public void setSeleccao(SortedSet<Pais> seleccao) {
		for (Pais t : seleccao)
			for (Equipa x : Equipa.allInstances)
				if (x.jogo() == this)
					x.setSeleccao(t);
	}

	/**********************************************************************
	 * User-defined operation specified in SOIL/OCL
	 * 
	 * @param data
	 *            the data to set
	 * @param hora
	 *            the hora to set
	 * @param visitado
	 *            the visitado to set
	 * @param visitante
	 *            the visitante to set
	 * @param estadio
	 *            the estadio to set
	 * @param grupo
	 *            the grupo to set
	 **********************************************************************/
	public void inicializa(String data, String hora, Pais visitado,
			Pais visitante, Estadio estadio, Grupo grupo) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	/**********************************************************************
	 * Object serializer
	 **********************************************************************/
	public String toString() {
		return "Jogo [dia=" + dia + ", hora=" + hora + ", grupo=" + grupo
				+ ", estadio=" + estadio + "]\n";
	}

	// Substituir o jogador 1 pelo jogador 2
	public void substituicao(Equipa e, Jogador j1, Jogador j2, int minEntrada,
			int minSaida) {

		if (e.getNumSubstituicoes() < 3) {
			if (equipa().contains(e) && e.membro().contains(j1)
					&& e.jogador().contains(j2)) {
				e.setSubstituicoes();
				e.membro().remove(j1);
				e.membro().add(new Membro(minEntrada, minSaida, e, j2));

			}
		}
	}

	// @pre dia!=null && hora !=null && dia!=this.dia || hora!=this.hora)
	public void adiarJogo(CalendarDate dia, String hora) {
		this.dia = dia;
		this.hora = hora;
	}

}
